package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zretc.dbtools.RandomIdTools;
import com.zretc.device.dao.Device_OutDao;
import com.zretc.device.entity.Device_Out;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class Device_OutAddDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtdevice_code;
	private JTextField txtdepartment;
	private JTextField txtout_date;
	private JTextField txtout_person;
	private JTextField txtout_num;
	private JTextField txttaker;
	private JTextField txtout_id;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Device_OutAddDialog dialog = new Device_OutAddDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Device_OutAddDialog() {
		setBounds(100, 100, 808, 608);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("出库添加");
		label.setFont(new Font("宋体", Font.PLAIN, 40));
		label.setBounds(304, 26, 182, 47);
		contentPanel.add(label);
		
		txtout_id = new JTextField();
		txtout_id.setText(RandomIdTools.getRan());
		txtout_id.setEditable(false);
		txtout_id.setFont(new Font("宋体", Font.PLAIN, 25));
		txtout_id.setColumns(10);
		txtout_id.setBounds(333, 93, 250, 30);
		contentPanel.add(txtout_id);
		
		JLabel label_8 = new JLabel("出库单号");
		label_8.setFont(new Font("宋体", Font.PLAIN, 25));
		label_8.setBounds(199, 93, 120, 30);
		contentPanel.add(label_8);

		txtdevice_code = new JTextField();
		txtdevice_code.setFont(new Font("宋体", Font.PLAIN, 25));
		txtdevice_code.setColumns(10);
		txtdevice_code.setBounds(333, 136, 250, 30);
		contentPanel.add(txtdevice_code);

		JLabel label_1 = new JLabel("设备编号");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(199, 136, 120, 30);
		contentPanel.add(label_1);

		txtdepartment = new JTextField();
		txtdepartment.setFont(new Font("宋体", Font.PLAIN, 25));
		txtdepartment.setColumns(10);
		txtdepartment.setBounds(333, 182, 250, 30);
		contentPanel.add(txtdepartment);

		JLabel label_2 = new JLabel("使用部门");
		label_2.setFont(new Font("宋体", Font.PLAIN, 25));
		label_2.setBounds(199, 182, 120, 30);
		contentPanel.add(label_2);

		txtout_date = new JTextField();
		txtout_date.setFont(new Font("宋体", Font.PLAIN, 25));
		txtout_date.setColumns(10);
		txtout_date.setBounds(333, 225, 250, 30);
		contentPanel.add(txtout_date);

		JLabel label_3 = new JLabel("出库时间");
		label_3.setFont(new Font("宋体", Font.PLAIN, 25));
		label_3.setBounds(199, 225, 120, 30);
		contentPanel.add(label_3);

		JLabel label_4 = new JLabel("出库状态");
		label_4.setFont(new Font("宋体", Font.PLAIN, 25));
		label_4.setBounds(199, 268, 133, 30);
		contentPanel.add(label_4);
		
		JComboBox txtout_status = new JComboBox();
		txtout_status.setFont(new Font("宋体", Font.PLAIN, 25));
		txtout_status.setModel(new DefaultComboBoxModel(new String[] {"正常", "损坏"}));
		txtout_status.setBounds(333, 268, 250, 30);
		contentPanel.add(txtout_status);

		txtout_person = new JTextField();
		txtout_person.setFont(new Font("宋体", Font.PLAIN, 25));
		txtout_person.setColumns(10);
		txtout_person.setBounds(333, 354, 250, 30);
		contentPanel.add(txtout_person);

		JLabel label_5 = new JLabel("出库数量");
		label_5.setFont(new Font("宋体", Font.PLAIN, 25));
		label_5.setBounds(199, 311, 106, 30);
		contentPanel.add(label_5);

		txtout_num = new JTextField();
		txtout_num.setFont(new Font("宋体", Font.PLAIN, 25));
		txtout_num.setColumns(10);
		txtout_num.setBounds(333, 311, 250, 30);
		contentPanel.add(txtout_num);

		JLabel label_6 = new JLabel("仓库管理员");
		label_6.setFont(new Font("宋体", Font.PLAIN, 25));
		label_6.setBounds(199, 354, 133, 30);
		contentPanel.add(label_6);

		JLabel label_7 = new JLabel("领 取 人");
		label_7.setFont(new Font("宋体", Font.PLAIN, 25));
		label_7.setBounds(199, 397, 120, 30);
		contentPanel.add(label_7);

		txttaker = new JTextField();
		txttaker.setFont(new Font("宋体", Font.PLAIN, 25));
		txttaker.setColumns(10);
		txttaker.setBounds(333, 397, 250, 30);
		contentPanel.add(txttaker);

		JButton button = new JButton("取消");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtout_id.setText(RandomIdTools.getRan());
				txtdevice_code.setText("");
				txtdepartment.setText("");
				txtout_date.setText("");
				txtout_num.setText("");
				txtout_person.setText("");
				txttaker.setText("");
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		button.setBounds(225, 459, 150, 38);
		contentPanel.add(button);

		JButton button_1 = new JButton("添加");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 获取输入的值
				String tout_id=txtout_id.getText();
				String device_code = txtdevice_code.getText();
				String department = txtdepartment.getText();
				String out_date = txtout_date.getText();
				String out_status =txtout_status.getSelectedItem()+"";
				String tout_num = txtout_num.getText();
				String out_person = txtout_person.getText();
				String taker = txttaker.getText();
				if (device_code.contentEquals("")) {
					//弹窗提示
					JOptionPane.showMessageDialog(null, "编号不能为空");
				}else if (department.equals("")) {
					//弹窗提示
					JOptionPane.showMessageDialog(null, "部门不能为空");
				}else if (out_date.equals("")) {
					//弹窗提示
					JOptionPane.showMessageDialog(null, "出库日期不能为空");
				}else if (out_status.equals("")) {
					//弹窗提示
					JOptionPane.showMessageDialog(null, "设备状态不能为空");
				}else if (tout_num.equals("")) {
					//弹窗提示
					JOptionPane.showMessageDialog(null, "出库数量不能为空");
				}else if (out_person.equals("")) {
					//弹窗提示
					JOptionPane.showMessageDialog(null, "仓库管理员不能为空");
				}else if (taker.equals("")) {
					//弹窗提示
					JOptionPane.showMessageDialog(null, "领取人不能为空");
				}else {
					//转型
					int out_id = Integer.parseInt(tout_id);
					int out_num=Integer.parseInt(tout_num);
					//创建对象
					Device_Out dout= new Device_Out(out_id, device_code, department, out_date, out_status, out_num, out_person, taker);
					Device_OutDao dod = new Device_OutDao();
					boolean flag=dod.addDevOut(dout);
					//对返回值进行判断
					if (flag) {
						//弹窗提示
						JOptionPane.showMessageDialog(null, "添加成功");
					}else {
						//弹窗提示
						JOptionPane.showMessageDialog(null, "添加失败,该单号已添加，请勿重复添加");
					}
				}
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));
		button_1.setBounds(420, 459, 150, 38);
		contentPanel.add(button_1);
		
		// 窗口居中
		setLocationRelativeTo(null);
	}
}
